package com.test.burnincash.aaahpoppomelo.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.test.burnincash.R;
import com.test.burnincash.aaahpoppomelo.activity.OrderDetailActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.base.BaseFragment_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.Order_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.UserData_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.util.http_hpoppomelo.HttpManager_hpoppomelo;

import java.util.ArrayList;


public class OrderListFragment_hpoppomelo extends BaseFragment_hpoppomelo {

    private ImageView noOrderIv;
    private LinearLayout orderGroupLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_list_hpoppomelo, container, false);
        init(view);
        return view;
    }

    private void init(View view){

        noOrderIv = view.findViewById(R.id.fragment_order_list_iv_no_order);
        orderGroupLayout = view.findViewById(R.id.fragment_order_list_layout_order_group);
    }

    @Override
    public void onResume() {
        super.onResume();
        HttpManager_hpoppomelo.getInstance().getData();
    }

    public void updateOrderList(){

        orderGroupLayout.removeAllViews();

        ArrayList<Order_hpoppomelo> orders = UserData_hpoppomelo.getInstance().getOrders();

        noOrderIv.setVisibility(orders.size()==0 ? View.VISIBLE:View.GONE);

        for (Order_hpoppomelo order:orders){
            showOrder(order);
        }

    }

    @SuppressLint("SetTextI18n")
    private void showOrder(Order_hpoppomelo order_hpoppomelo){

        @SuppressLint("InflateParams") View orderView = LayoutInflater.from(requireContext()).inflate(R.layout.item_order_hpoppomelo,null);

        TextView orderNumTv = orderView.findViewById(R.id.view_order_tv_order_num);
        Button orderDetailBtn = orderView.findViewById(R.id.view_order_btn);
        TextView dateTv = orderView.findViewById(R.id.view_order_tv_date);
        TextView customerTv = orderView.findViewById(R.id.view_order_tv_customer);

        orderNumTv.setText(String.valueOf(order_hpoppomelo.getOrderNumber()));
        dateTv.setText(order_hpoppomelo.getDate());
        customerTv.setText(order_hpoppomelo.getCustomer());
        orderDetailBtn.setText("Orderdetail");
        orderDetailBtn.setOnClickListener(v -> {
            Order_hpoppomelo.optionOrder = order_hpoppomelo;
            Intent intent = new Intent(requireContext(), OrderDetailActivity_hpoppomelo.class);
            requireContext().startActivity(intent);
        });

        orderGroupLayout.addView(orderView);
    }

}